/**
 * Created by smp on 2017/3/15.
 */
var isSearchBarExpand = false;
var index;

var modelPath=ROOT_PATH+'/permission/dictionaryType/';
function doSearch(value, name) {
    alert('You input: ' + value + '(' + name + ')');
}
function toggleSearch() {
    var $mainLayout = $('#main_layout');
    var expandHeight = 200;
    var collapseHeight = 50;
    if (!isSearchBarExpand){
        $mainLayout.layout('panel','north').panel('resize',{height:expandHeight});
        $mainLayout.layout('resize');
        isSearchBarExpand = true;
    }
    else {
        $mainLayout.layout('panel','north').panel('resize',{height:collapseHeight});
        $mainLayout.layout('resize');
        isSearchBarExpand = false;
    }
}
/**
 * 关闭layer
 */
function closeLayer() {
    layer.close(index);
}
/**
 * 使用layer 打开一个页面
 * @param title
 * @param content
 */
function openLayer(title,content) {

    index=layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        area: ['600px', '380px'],
        content: modelPath+content
    });
}
function result(data) {
    if(data==1){
        layer.msg("保存成功");
        closeLayer();
    }else{
        layer.alert("保存失败");
    }
}
$(function () {
    $('#dictionaryTypeList').datagrid({
        method:'post',
        iconCls: 'icon-edit',//图标
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect:true,
        fitColumns: true,
        pagination: true,
        url: ROOT_PATH + '/permission/dictionaryType/list.shtml',
        queryParams: {
            sort: 'sort',
            order: 'asc'
        },
        columns:[[
            {field: 'id',checkbox: true},
            {field:'code',title:'字典类型标识',width:100,sortable:true},
            {field:'name',title:'字典类型名称',width:100,sortable:true},
            {
                field: 'readonly',
                title: '是否可编辑',
                width: 100,
                sortable: true,
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return '是';
                    } else {
                        return '否';
                    }
                }
            },
            {field:'remarks',title:'备注',width:100,sortable:true}
            /*{field:'useState',title:'启用状态',width:100,formatter: function (value, row, index) {
             return row.useState == '0' ? '启用' : '未启用';
             }}*/
        ]],
        toolbar: '#toolbar'
    });
    /**
     * 查询按钮
     */
    $('#selectDictionaryType').on('click',function(){
        var name=$('#name').val();

        $('#dictionaryTypeList').datagrid('load', {name:name,sort: 'sort', order: 'asc'});
    });

        //添加按钮事件
        $('#btn_dictionaryType_add').on('click',function () {

            var title='添加字典类型';
            var content='addDictionary.shtml';
            openLayer(title,content);
        });
        //编辑按钮事件
        $('#btn_dictionaryType_edit').on('click',function () {

            var checkedRows=$('#dictionaryTypeList').datagrid('getChecked');

            if(checkedRows.length == 0){
                layer.alert("选择需要编辑的行！");
                return
            }else if (checkedRows.length > 1){
                layer.alert("只能选中一行数据！");
                return
            }
            var readonly=checkedRows[0].readonly;
            if(readonly==0){
                layer.alert("不可编辑！");
                return
            }
            var id=checkedRows[0].id;
            var title='编辑字典类型';
            var content='editDictionaryType.shtml?id='+id;
            openLayer(title,content);
        });
        //删除按钮事件
        $('#btn_dictionaryType_delete').on('click',function () {

            var checkedRows=$('#dictionaryTypeList').datagrid('getChecked');

            if(checkedRows.length == 0){
                layer.alert("选择需要删除的行！");
                return
            }else if (checkedRows.length > 1){
                layer.alert("只能选中一行数据！");
                return
            }
            var readonly=checkedRows[0].readonly;
            if(readonly==0){
                layer.alert("不可编辑！");
                return
            }
            var id=checkedRows[0].id;
            layer.confirm('是否确定删除当前选中数据？', {
                btn: ['确定','取消'] //按钮
            }, function(){
            $.ajax({
                type: "POST",
                url: ROOT_PATH+'/permission/dictionary/deleteDictionaryTypeJudgement.shtml',
                data: {typeId:id},
                dataType: "json",
                success: function(result){
                    if (result.success) {
                            $.ajax({
                                type: "POST",
                                url: ROOT_PATH+'/permission/dictionaryType/deleteDictionaryType.shtml',
                                data: {id:id,delState:1},
                                dataType: "json",
                                success: function(result){
                                    if (result.success) {
                                        $('#dictionaryTypeList').datagrid('clearSelections').datagrid('reload');
                                        layer.msg('删除成功', {
                                            icon: 1,
                                            time: 1000
                                        });
                                    }
                                    else {
                                        layer.msg('删除失败', {
                                            icon: 2,
                                            time: 2000
                                        });
                                    }
                                }
                            });
                    }
                    else {
                        layer.msg('请先删除该类型下的字典', {
                            icon: 2,
                            time: 2000
                        });
                    }
                }
            });
            });
        });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
    });
});